﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using cmdb.keede.com.Models;
using cmdb.keede.common;
using cmdb.keede.entity;
using cmdb.keede.service;
using Microsoft.AspNetCore.Mvc;

namespace cmdb.keede.com.Controllers
{
    public class BusinessTopologyController : Controller
    {
        readonly IBusinessTopologyService _businessTopoService;
        readonly IVirtualHostInfoService _virtualHostInfoService;

        private readonly IMapper _mapper;

        public BusinessTopologyController(IBusinessTopologyService businessTopoService, IMapper mapper, IVirtualHostInfoService virtualHostInfoService)
        {
            _businessTopoService = businessTopoService;
            _mapper = mapper;
            _virtualHostInfoService = virtualHostInfoService;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<BizResult<List<TreeNode>>> GetTreeNodes(int level, int parentId, int bakValue)
        {
            return await _businessTopoService.GetTreeNodesAsync(level, parentId, bakValue);
        }

        public async Task<BizResult<List<VirtualHostInfoViewModel>>> GetVirtualHostsByAppId(int applicationId, int pageIndex, int pageSize)
        {
            var result = await _businessTopoService.GetVirtualHostsByAppIdAsync(applicationId, pageIndex, pageSize);
            return new BizResult<List<VirtualHostInfoViewModel>>(
                result.ReturnObj.Select(s => _mapper.Map<VirtualHostInfoViewModel>(s)).ToList(),
                result.BusinessCode,
                result.BusinessMessage,
                result.Pager);
        }

        public async Task<BizResult<List<VirtualHostInfoViewModel>>> SearchVirtualHosts(int applicationId, string innerIP, string outerIP, EnumOperationSystem? os, EnumVirtualHostStatus? status, int pageIndex, int pageSize)
        {
            var result = await _businessTopoService.SearchVirtualHostsAsync(applicationId, innerIP, outerIP, os, status, pageIndex, pageSize);
            return new BizResult<List<VirtualHostInfoViewModel>>(
                result.ReturnObj.Select(s => _mapper.Map<VirtualHostInfoViewModel>(s)).ToList(),
                result.BusinessCode,
                result.BusinessMessage,
                result.Pager);
        }

        public async Task<BizResult<VirtualHostInfoViewModel>> GetVirtualHostById(int id)
        {
            var biz = await _virtualHostInfoService.GetVirtualHostInfoAsync(id);
            return new BizResult<VirtualHostInfoViewModel>(_mapper.Map<VirtualHostInfoViewModel>(biz.ReturnObj));
        }
    }
}